package com.cb.controller.admin.system;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import com.cb.common.util.MenuUtil;
import com.cb.core.Constants;
import com.cb.core.controller.BaseListController;
import com.cb.core.spring.ex.security.FilterInvocationDefinitionSource;
import com.cb.domain.model.system.AppResource;
import com.cb.domain.model.system.AppRole;
import com.cb.domain.service.system.AppResourceManager;
import com.cb.domain.service.system.AppRoleManager;

/**
 * 权限授予
 * @author Administrator
 *
 */
public class GrantRightController extends BaseListController {
	@Autowired
	private AppRoleManager appRoleManager;
	@Autowired
	private AppResourceManager appResourceManager;
	@Autowired
	private FilterInvocationDefinitionSource definitionSource;

	@Override
	public ModelAndView defaultAction(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String roleId=request.getParameter("roleId");
		AppRole role=appRoleManager.get(new Long(roleId));
		
		AppResource rootResource=appResourceManager.get(Constants.ROOT_RESOURCE_PK);
		//rootResource can't not be null.
		
		StringBuffer sb=new StringBuffer();
		sb.append("<ul id='rightTreeview' class='filetree'>\t");
		//sb.append("<span class='folder'><input type='checkbox'>"+rootResource.getResourceTitle()+"</span>\t");
		generateMenuItem(sb,rootResource);
		sb.append("</ul>");	

		request.setAttribute("treeMenus", sb.toString());
		
		return new ModelAndView("admin/system/grantRight","appRole",role);
	}
	
	/**
	 * genenate the ul html as below from app_resource table 
	 * <pre>
	 * <ul id="rightTreeview" class="filetree">
	 *		<li><span class="folder"><input type="checkbox"/>超级权限</span>
	 *			<ul>
	 *				<li><span class="folder"><input type="checkbox"/>Folder 1</span>
	 *					<ul>
	 *						<li><span class="file"><input type="checkbox"/>Item 1.1</span></li>
	 *					</ul>
	 *				</li>
	 *				<li><span class="folder"><input type="checkbox"/>Folder 2</span>
	 *				</li>
	 *			</ul>
	 *		</li>
	 *	</ul>
	 * </pre>
	 * @param sb
	 * @param resource
	 */
	private void generateMenuItem(StringBuffer sb,AppResource resource){
		
		List subResources=appResourceManager.getByParentId(resource.getResourceId());

		sb.append("<li>");
		
		if(subResources.size()>0){
			sb.append("<span class='folder'>");
		}else{
			sb.append("<span class='file'>");
		}
		
		sb.append("<input type='checkbox' name='resourceId' value='"+resource.getResourceId()+"' path='"+resource.getPath()+"'>");
		sb.append("<a href='javascript:;' title='"+resource.getResourceDesc()+"'>"+resource.getResourceTitle()+"</a>");sb.append("</span>");
		
		if(subResources.size()>0){
			sb.append("<ul>\t");
			for(int i=0;i<subResources.size();i++){
				AppResource rs=(AppResource)subResources.get(i);
				generateMenuItem(sb, rs);
			}
			sb.append("</ul>\t");
		}
		
		sb.append("</li>");
	}
	
	public ModelAndView saveGrantRight(HttpServletRequest request,HttpServletResponse response){
		
		String[] resourceIds=request.getParameterValues("resourceId");
		String roleId=request.getParameter("roleId");
		AppRole appRole=appRoleManager.get(new Long(roleId));
		
		if(resourceIds!=null){
			appRole.getResources().clear();
			for(int i=0;i<resourceIds.length;i++){
				AppResource appResource=appResourceManager.get(new Long(resourceIds[i]));
				appRole.getResources().add(appResource);
			}
			appRoleManager.save(appRole);
			//重新加载权限
			definitionSource.reloadMappings();
			
			MenuUtil.resetMenuRole(request);
		}
		
		saveMessage(request, getText("appRole.grantRight", new Object[]{appRole.getRoleName()}));
		
		return new ModelAndView("redirect:appRoles.do");
	}
}
